package com.bw.day03.order

import androidx.lifecycle.viewModelScope
import com.bw.base.net.entity.CreateOrderEntity
import com.bw.base.net.entity.GetPreOrderEntity
import com.bw.base.net.entity.OrderEntity
import com.bw.base.net.entity.PayEntity
import com.bw.base.net.entity.Res
import com.bw.base.net.mvvm.BaseViewModel
import com.bw.base.net.param.ParamCreateOrder
import com.bw.base.net.param.ParamPay
import com.bw.day03.utlis.SingleLiveData
import kotlinx.coroutines.launch

class OrderViewModel: BaseViewModel() {
    val getSuccess=SingleLiveData<Res<GetPreOrderEntity>>()
    val getFail=SingleLiveData<String>()
    fun getPreOrder(preOrderNo: String) {
        viewModelScope.launch {
            try {
                getSuccess.postValue(apiService.getPreOrder(preOrderNo))
            }catch (e:Exception){
                getFail.postValue(e.message.toString())
            }
        }
    }
    val createSuccess = SingleLiveData<Res<CreateOrderEntity>>()
    val createFail = SingleLiveData<String>()
    fun createOrder(paramCreateOrder: ParamCreateOrder){
        viewModelScope.launch {
            try {
                createSuccess.postValue(apiService.createOrder(paramCreateOrder))
            }catch (e:Exception){
                createFail.postValue(e.message.toString())
            }
        }
    }
    val paySuccess = SingleLiveData<Res<PayEntity>>()
    val payFail = SingleLiveData<String>()
    fun pay(paramPay: ParamPay){
        viewModelScope.launch {
            try {
                paySuccess.postValue(apiService.pay(paramPay))
            }catch (e:Exception){
                payFail.postValue(e.message.toString())
            }
        }
    }
    val orderSuccess=SingleLiveData<Res<OrderEntity>>()
    val orderFail=SingleLiveData<String>()
    fun order(type: String,limit: Int,page: Int){
        viewModelScope.launch {
            try {
                orderSuccess.postValue(apiService.order(type,limit,page))
            }catch (e:Exception){
                orderFail.postValue(e.message.toString())
            }
        }
    }
}